/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package controller.javaBeans;

import java.util.ArrayList;
import java.util.Date;

import model.Vocabulary;
import model.dataContainer.DiscussionContainer;

import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import controller.sesameDataConnector.UserDiscussionConnector;

public class UserDiscussionBean {

	private Date id;
	private URI user;
	private URI cr;
	private String comment;
	
	
	public UserDiscussionBean(){
		id = null;
		user = null;
		cr = null;
		comment = "";
	}
	
	public boolean insert(String crid, String id, URI user) {
		try {
			model.dataContainer.DiscussionContainer ud = new DiscussionContainer();
			ud.setComment(this.comment);
			ud.setCr(new URIImpl((Vocabulary.NAMESPACE.stringValue()) + id  + "/ChangeRequest/" + crid));
			ud.setId(new Date());
			ud.setUser(user);
			

			URI uri = new URIImpl(Vocabulary.NAMESPACE.stringValue() + id + "/ChangeRequest/" + crid + "/" + Long.toString(ud.getId().getTime()) );
			controller.sesameDataConnector.UserDiscussionConnector.insert(uri, ud);
			
		} catch (Exception e){

			return false;
		}
		return true;
	}
	
	
	
	public String selectAllOfElement(String elementid, String crid){
		String ret = "";
		
		URI uri = new URIImpl(Vocabulary.NAMESPACE.stringValue() + elementid + "/ChangeRequest/" + crid);
		ArrayList<DiscussionContainer> ud = UserDiscussionConnector.selectAllOfElement(uri);
		
		if (ud == null) {
			ret = "no comments added till now";
		}
		else{	
			for (int i = 0; i < ud.size(); i++){
				ret = 
					"<div>User " + ud.get(i).getUser().getLocalName() + " wrote at " + ud.get(i).getId().toString() + "</div>" + 
					"<div class=\"relatedElementGroup\">" + ud.get(i).getComment() + "</div>" + ret;
			}
		}
		
		return ret;
		
	}
	
	
	public String getComment() {
		return comment;
	}
	public URI getCr() {
		return cr;
	}
	public Date getId() {
		return id;
	}
	public URI getUser() {
		return user;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}
	public void setCr(URI cr) {
		this.cr = cr;
	}
	public void setId(Date id) {
		this.id = id;
	}
	public void setUser(URI user) {
		this.user = user;
	}
	
	
	
	
}
